package com.java8feature.java8action.chapter11;

import com.java8feature.java8action.chapter11.entity.Shop;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/6/16 16:31
 */
public class CompletableFutureThreadCoreTest {

    //

//    public String decorate(String prodcut) {
//        getPrice(prodcut);
//    }

    public static void main(String[] args) {
        CompletableFutureThreadCoreTest test = new CompletableFutureThreadCoreTest();
        List<Shop> shops = Arrays.asList(new Shop("orange"),
                      new Shop("apple1")
                    , new Shop("apple2")
                    ,new Shop("apple3")
                    , new Shop("apple4")
                    , new Shop("apple5")
                    , new Shop("apple6")
                    , new Shop("apple7")
                    , new Shop("apple8")
                , new Shop("apple8")
                        , new Shop("apple7")
                , new Shop("apple7")
                , new Shop("apple7"));

        CompletableFuture completableFuture = new CompletableFuture();


        System.out.println("============================GStartTime============================");
        long startTime = System.currentTimeMillis();
        List<Future<String>> completableFutures =
                shops.stream()
                        .map(shopStream -> {
                            return CompletableFuture.supplyAsync(
                                    () -> {
                                        try {
                                            System.out.println(Thread.currentThread().getName());
                                            return shopStream.getName() + "Price is" + shopStream.getPrice(shopStream.getName());
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        } finally {
                                            //return CompletableFuture.completedFuture("Exception Value");
                                            return null;
                                        }
                                    }
                            );
                        })
                        .collect(Collectors.toList());

                        List<String> prices = completableFutures.stream()
                                .map(f -> {
                                    try {
                                        return f.get();
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    } catch (ExecutionException e) {
                                        e.printStackTrace();
                                    } finally {
                                        return null;
                                    }
                                })
                                .collect(Collectors.toList());


        System.out.println(System.currentTimeMillis() - startTime);
        System.out.println("============================GEndTime============================");


        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}
